package com.zhl.config;

import feign.*;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.cloud.openfeign.CircuitBreakerNameResolver;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignCircuitBreaker;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.List;
import java.util.stream.Collectors;

public class FeignLocal {

    public static FeignLocal.Builder builder() {
        return new FeignLocal.Builder();
    }

    /**
     * Builder for Feign CircuitBreaker integration.
     */
    public static final class Builder extends Feign.Builder {

        private DispatcherServlet dispatcherServlet;

        Builder dispatcherServlet(DispatcherServlet dispatcherServlet) {
            this.dispatcherServlet = dispatcherServlet;
            return this;
        }

        @Override
        public <T> T target(Target<T> target) {
            return build(dispatcherServlet).newInstance(target);
        }

        /**
         *
         * @return
         */

        public Feign build(DispatcherServlet dispatcherServlet) {
            super.invocationHandlerFactory((target, dispatch) -> new FeignLocalInvocationHandler(target, dispatch, dispatcherServlet));

////            ReflectiveFeign reflectiveFeign = (ReflectiveFeign) super.build();
//            Client client = (Client)Capability.enrich(this.client, this.capabilities);
//            Retryer retryer = (Retryer)Capability.enrich(this.retryer, this.capabilities);
//            List<RequestInterceptor> requestInterceptors = (List)this.requestInterceptors.stream().map((ri) -> {
//                return (RequestInterceptor)Capability.enrich(ri, this.capabilities);
//            }).collect(Collectors.toList());
//            Logger logger = (Logger)Capability.enrich(this.logger, this.capabilities);
//            Contract contract = (Contract)Capability.enrich(this.contract, this.capabilities);
//            Request.Options options = (Request.Options)Capability.enrich(this.options, this.capabilities);
//            Encoder encoder = (Encoder)Capability.enrich(this.encoder, this.capabilities);
//            Decoder decoder = (Decoder)Capability.enrich(this.decoder, this.capabilities);
//            InvocationHandlerFactory invocationHandlerFactory = (InvocationHandlerFactory)Capability.enrich(this.invocationHandlerFactory, this.capabilities);
//            QueryMapEncoder queryMapEncoder = (QueryMapEncoder)Capability.enrich(this.queryMapEncoder, this.capabilities);
//            SynchronousMethodHandler.Factory synchronousMethodHandlerFactory = new SynchronousMethodHandler.Factory(client, retryer, requestInterceptors, logger, this.logLevel, this.decode404, this.closeAfterDecode, this.propagationPolicy, this.forceDecoding);
//            ReflectiveFeign.ParseHandlersByName handlersByName = new ReflectiveFeign.ParseHandlersByName(contract, options, encoder, decoder, queryMapEncoder, this.errorDecoder, synchronousMethodHandlerFactory);
//            return new ReflectiveFeign(handlersByName, invocationHandlerFactory, queryMapEncoder);

            return super.build();
        }

    }

}
